package com.xiaoyu.merchant.ui.activity.login;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.socialize.UMShareAPI;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.utils.CheckFormatUtils;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.StatusBarUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.network.NetworkManager;
import com.xiaoyu.merchant.ui.activity.ApplyShopActivity;
import com.xiaoyu.merchant.ui.fragment.main.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.activity_login_register)
    TextView mRegister;
    @BindView(R.id.activity_login_btn)
    Button mLogin;
    @BindView(R.id.activity_login_forget_pwd)
    TextView mForgetPwd;

    @BindView(R.id.login_please_input_mobile)
    EditText mMobile;
    @BindView(R.id.login_please_input_pwd)
    EditText mPwd;
    @BindView(R.id.actvitiy_login_wechat)
    LinearLayout mWechatLinear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        StatusBarUtil.StatusBarLightMode(this);
        setOccupyViewLinearGradientLogin();
        checkPermision();
        initView();
    }

    private void initView() {
        mWechatLinear.setVisibility(View.GONE);
        mPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    //注册账号
    @OnClick(R.id.activity_login_register)
    public void mRegisterClick() {
        startActivity(new Intent(this, RegisterActivity.class));
    }

    //登录
    @OnClick(R.id.activity_login_btn)
    public void mLoginClick() {
        if (TextUtils.isEmpty(mMobile.getText().toString()) || TextUtils.isEmpty(mPwd.getText().toString())) {
            ToastUtil.showToast(this, "请输入完整信息");
            return;
        }
        if (CheckFormatUtils.checkMobilePhone(mMobile.getText().toString()) == 2) {
            ToastUtil.showToast(this, "手机号不合法");
            return;
        }

        login();

    }

    //调登录接口
    private void login() {
        NetworkManager.login(mMobile.getText().toString(), mPwd.getText().toString(), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ToastUtil.showToast(LoginActivity.this, "登录成功");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONObject data = jsonObject.getJSONObject("data");
                    PreferencesUtil.commit(PreferencesUtil.TOKEN, data.getString("token"));//用于判断是否登录
                    String storestate = data.getString("storestate");//开店是否审核通过

                    PreferencesUtil.commit(PreferencesUtil.LOGIN_TYPE, 1);//登录方式
                    PreferencesUtil.commit(PreferencesUtil.USERNAME, mMobile.getText().toString());//登录账号
                    PreferencesUtil.commit(PreferencesUtil.PASSWORD, mPwd.getText().toString());//登录密码
                    PreferencesUtil.commit(PreferencesUtil.STORESTATE, storestate);//是否申请开店成功
                    Log.i("jx", "onSuccess: " + storestate);
                    if (storestate.equals("3")) {  //审核通过
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    } else { //审核未通过，开店申请中
                        startActivity(new Intent(LoginActivity.this, ApplyShopActivity.class));
                    }

                    finish();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                if (errorCode.equals("y403")) {
                    ToastUtil.showToast(LoginActivity.this, "用户名或密码错误");
                    return;
                }
            }
        });
    }

    //忘记密码
    @OnClick(R.id.activity_login_forget_pwd)
    public void forgetPwd() {
        startActivity(new Intent(this, ForgetPwdActivity.class));
    }

    @OnClick(R.id.title_return)
    public void returnClick() {
        finish();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }


    /**
     * Android 6.0权限适配
     */
    private void checkPermision() {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.READ_LOGS,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.SET_DEBUG_APP,
                    Manifest.permission.SYSTEM_ALERT_WINDOW,
                    Manifest.permission.GET_ACCOUNTS,
                    Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
    }

    /**
     * 权限添加回调
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

    }

}
